using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//生成的残影预制体控制
public class Shadow : MonoBehaviour
{
    private Transform player;//玩家角色
    private SpriteRenderer thisSprite;//残影图片
    private SpriteRenderer playerSprite;//角色图片
    // private float alpha;
    public float fadeDuration = 1.0f; // 残影淡出的持续时间，单位秒
    public Color color;//残影颜色

    private float startTime;

    private void OnEnable()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        thisSprite = GetComponent<SpriteRenderer>();
        playerSprite = player.GetComponent<SpriteRenderer>();
        startTime = Time.time;
        color.a = 1;
        // alpha = 1;
        //设置残影图片为角色当前的图片
        thisSprite.sprite = playerSprite.sprite;
        //设置残影的位置为角色当前的位置
        transform.position = player.position;
        //设置残影的翻转为角色当前的翻转
        transform.localScale = player.localScale;
    }

    void FixedUpdate()
    {
        float elapsed = Time.time - startTime;
        float fadeAmount = 1.0f - Mathf.Clamp01(elapsed / fadeDuration); // 计算淡出的透明度

        // Color color = new Color(0.5f, 0.5f, 1, alpha);
        color.a = fadeAmount; // 设置新的透明度
        thisSprite.color = color;

        if (fadeAmount <= 0.0f)
        {
            //返回对象池
            ObjectPool.PushObject(gameObject);
        }
    }
}
